package com.css.fxfzaqbz.modules.data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.data.entity.BigDisposeManager;
import com.css.fxfzaqbz.modules.data.entity.DisposeManager;
import com.css.fxfzaqbz.modules.data.entity.ImportantActivity;
import com.css.fxfzaqbz.modules.data.queryParams.DisposeManagerQueryParams;
import com.css.fxfzaqbz.modules.data.queryParams.DisposeVo;
import com.css.fxfzaqbz.modules.data.repository.ActiveFaultDataRepository;
import com.css.fxfzaqbz.modules.data.repository.BigDisposeManagerRepository;
import com.css.fxfzaqbz.modules.data.repository.DisposeManagerRepository;
import com.css.fxfzaqbz.modules.data.service.DisposeExportService;
import com.css.fxfzaqbz.modules.data.service.DisposeManagerService;
import com.css.fxfzaqbz.modules.data.service.ImportantActivityService;
import com.css.fxfzaqbz.util.*;
import com.css.fxfzaqbz.web.PlatformSessionContext;
import com.css.fxfzaqbz.zcpt.sys.entity.SUser;
import com.css.fxfzaqbz.zcpt.sys.service.SUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;


@Service
public class DisposeManagerServiceImpl implements DisposeManagerService {

    @Resource
    ImportantActivityService importantActivityService;
    @Resource
    DisposeManagerRepository disposeManagerRepository;
    @Resource
    SUserService sUserService;
    @Resource
    BigDisposeManagerRepository bigDisposeManagerRepository;

    @Override
    public void saveDisposeManager(DisposeManager disposeManager) {
        if (disposeManager.getIaId() == null) {
            disposeManager.setIaName("系统默认");
        } else {
            ImportantActivity importantActivity = importantActivityService.getImportantActivityById(disposeManager.getIaId());
            disposeManager.setIaName(importantActivity.getIaName());
        }
        disposeManager.setDmId(UUIDGenerator.getUUID());
        disposeManager.setDelFlag(YNEnum.N.toString());
        disposeManager.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        disposeManager.setUploadTime(PlatformDateUtils.getCurrentTimestamp());
        String userId = PlatformSessionContext.getUserID();
        String user = sUserService.getSUser(userId);
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        disposeManager.setAdmin(sUser.getUserName());
        disposeManager.setCreateUser(userId);
        disposeManager.setTaskName("处置要点分析");
        disposeManager.setFlag("0");
        disposeManagerRepository.saveDisposeManager(disposeManager);
    }

    @Override
    public DisposeManager getDisposeManager(String id) {
        return disposeManagerRepository.getDisposeManager(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delDisposeManager(List<String> ids) {
        Timestamp timestamp = PlatformDateUtils.getCurrentTimestamp();
        String userId = PlatformSessionUtils.getUserId();
        disposeManagerRepository.delDisposeManager(ids, timestamp, userId);
    }

    @Override
    public void updateDisposeManager(DisposeManager disposeManager) {
        disposeManager.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        disposeManager.setUpdateUser(PlatformSessionUtils.getUserId());
        if (disposeManager.getIaId() == null) {
            disposeManagerRepository.updateDisposeManager(disposeManager);
            return;
        }
        disposeManager.setIaId(null);
        saveDisposeManager(disposeManager);


    }

    @Override
    public Map<String, Object> getAllDisposeManager(DisposeManagerQueryParams queryParams) {
        return disposeManagerRepository.getAllDisposeManagerParams(queryParams);
    }

    @Override
    public void updateDisposeFX(DisposeManager disposeManager) {
        disposeManager.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        disposeManager.setUpdateUser(PlatformSessionUtils.getUserId());
        disposeManagerRepository.updateDisposeManager(disposeManager);
    }

    @Override
    public BigDisposeManager getDisposeManagerByIaId(String id,int curPage,int pageSize) {
        String a="处置要点分析";
        BigDisposeManager bigDisposeManager = bigDisposeManagerRepository.getTask(id, a);
        ImportantActivity importantActivity = importantActivityService.getImportantActivityById(id);
        bigDisposeManager.setIaOpinion(importantActivity.getIaOpinion());
        bigDisposeManager.setIaOpinionStatus(importantActivity.getIaOpinionStatus());
        Map<String, Object> map = disposeManagerRepository.getDisposeManagerByIaId(id, curPage, pageSize);

        bigDisposeManager.setMap(map);
        return bigDisposeManager;
    }

    @Override
    public void saveList(DisposeVo disposeVo) {
        ImportantActivity importantActivity = importantActivityService.getImportantActivityById(disposeVo.getIaId());
        for (String id : disposeVo.getIds()) {
            DisposeManager disposeManager = disposeManagerRepository.getDisposeManager(id);
            disposeManager.setDmId(UUIDGenerator.getUUID());
            disposeManager.setIaId(disposeVo.getIaId());
            disposeManager.setIaName(importantActivity.getIaName());
            disposeManager.setUploadTime(PlatformDateUtils.getCurrentTimestamp());
            disposeManager.setCreateUser(PlatformSessionUtils.getUserId());
            disposeManager.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
            String user = sUserService.getSUser(PlatformSessionUtils.getUserId());
            SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
            disposeManager.setAdmin(sUser.getUserName());
            disposeManagerRepository.saveDisposeManager(disposeManager);
        }
    }


    @Override
    public void updateSubmitButton(List<String> ids) {
        String userID = PlatformSessionContext.getUserID();
        String userJson = sUserService.getSUser(userID);
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
        //当前用户id
        String userName = sUser.getUserName();
        //当前用户的联系方式
        String mobilePhone = sUser.getMobilePhone();
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        disposeManagerRepository.updateSubmitButton(ids,userName,mobilePhone,currentTimestamp);
    }
}